/**
 * 
 * purpose: Scrollpane...
 * @auth: Ning
 * date: 2009-9-16
 * 
 * */
package com.components.scrollpane
{
	import core.section.Controller;
	
	import debug.Log;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	public class ScrollPane extends Controller
	{
		//START...
		
		//PROPERTY...
		private var tar:Sprite;
		
		private var conMsk:Sprite;
		
		private var container:Sprite;
					
		private var _content:DisplayObject;
				
		private var scrollV:ScrollBar;
		
		private var scrollH:ScrollBar;
		
		private var vScroll:Sprite;
		
		private var hScroll:Sprite;
				
		
		//CONSTRUCTOR...
		public function ScrollPane(target:DisplayObject)
		{
			
			super(target);
						
			tar = target as Sprite;
			
			init();
			
		}
		
		//METHODS...
		
		//init...
		private function init():void
		{
			//set variable value...
			for(var i:uint = 0;i < tar.numChildren;i++)
			{
				
				try{
										
					this[tar.getChildAt(i).name] = tar.getChildAt(i);
										
				}catch(err:Error){
					
					trace("Instance Miss: " + tar.getChildAt(i).name);
					
				}
				
			}
			
			container.mask = conMsk;
			
			if(hScroll)
			{
				
				hScrollBar = new ScrollBar(hScroll as Sprite);
				
			}
			
			if(vScroll)
			{
				
				vScrollBar = new ScrollBar(vScroll as Sprite,true);
				
			}
			
			
		}
		
		//h position change...
		private function hPositionChange(evt:ScrollPaneEvents):void
		{
			
			var pos:Number = evt.position;
			
			var rect:Rectangle = content.getBounds(content);
			
			var conWidth:Number = rect.x + rect.width - conMsk.width;
						
			container.x = conMsk.x -  conWidth * pos ;	
			
		}
		
		//v position change...
		private function vPositionChange(evt:ScrollPaneEvents):void
		{
			
			var pos:Number = evt.position;
			
			var rect:Rectangle = content.getBounds(content);
			
			var conHeight:Number = rect.y + rect.height - conMsk.height;
						
			container.y = conMsk.y -  conHeight * pos ;	
			
		}		
		
		
		//set content...
		public function set content(con:DisplayObject):void
		{
			
			if(content is DisplayObject)
			{
				
				container.removeChild(content);
				
			}
				
			_content = con;
			
			content.x = 0;
			
			content.y = 0;
						
			container.addChild(con);
			
			update();
			
		}
		
		
		//get content...
		public function get content():DisplayObject
		{
			
			return _content; 
			
		}
		
		
		
		//update...
		public function update():void
		{
						
			if(scrollH is ScrollBar) scrollH.update(conMsk.width,content.width);
									
			if(scrollV is ScrollBar) scrollV.update(conMsk.height,content.height);
						
		}
		
		//set hscroll bar...
		public function set hScrollBar(val:ScrollBar):void
		{
			
			scrollH = val;
			
			if(content is DisplayObject)
			{
				
				var rect:Rectangle = content.getBounds(content);
				
				scrollH.update(conMsk.height,rect.x + rect.width);
				
				
			}
			
			scrollH.addEventListener(ScrollPaneEvents.POSITION_CHANGE,hPositionChange);
					
		}
		
		//get hScroll bar...
		public function get hScrollBar():ScrollBar
		{
			
			return scrollH;
			
		}
		
		//set vscroll bar...
		public function set vScrollBar(val:ScrollBar):void
		{
			
			scrollV = val;
			
			if(content is DisplayObject)
			{
				
				var rect:Rectangle = content.getBounds(content);
				
				scrollV.update(conMsk.height,rect.y + rect.height);
				
				
			}
							
			scrollV.addEventListener(ScrollPaneEvents.POSITION_CHANGE,vPositionChange);
			
		}
		
		
		//get vScroll bar
		public function get vScrollBar():ScrollBar
		{
			
			return scrollV;
			
		}
				
		
		///END
	}
}